//
//  NearDriverView.m
//  easyTaxi
//
//  Created by ellenpage on 12-12-25.
//  Copyright (c) 2012年 ellenpage. All rights reserved.
//

#import "Constant.h"
#import "NearDriverView.h"
#import "NearDriverCell.h"
#import "PassValueClass.h"
#import <QuartzCore/QuartzCore.h> //导入框架

@interface NearDriverView ()

@property (weak, nonatomic) IBOutlet UILabel *nameLbl;
@property (weak, nonatomic) IBOutlet UILabel *statusLbl;
@property (weak, nonatomic) IBOutlet UILabel *carNoLbl;
@property (weak, nonatomic) IBOutlet UILabel *creditLbl;
@property (weak, nonatomic) IBOutlet UILabel *goodEvaluationLbl;
@property (weak, nonatomic) IBOutlet UILabel *badEvaluationLbl;
@property (weak, nonatomic) IBOutlet UIView *detaiView;
@property (weak, nonatomic) IBOutlet UIView *detaiView_Small;
@property (weak, nonatomic) IBOutlet UIButton *callDriverBtn;

- (IBAction)goBack;
- (IBAction)removeThisView;
- (IBAction)callThisDriver:(UIButton *)sender;

@end

@implementation NearDriverView

#pragma mark - System Function - 系统函数

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self = [[[NSBundle mainBundle] loadNibNamed:@"NearDriverView" owner:self options:nil] objectAtIndex:0];
        [self loadData];//加载数据
    }
    return self;
}

#pragma mark - Goback - 返回

- (IBAction)goBack
{
    [self removeFromSuperview];
}


#pragma mark - LoadData - 加载数据

- (void)loadData
{
    _nearDriverTBView.dataSource = self;
    _nearDriverTBView.delegate   = self;
    _nearDriverTBView.backgroundColor = [UIColor clearColor];
    
    _dataArray = [NSMutableArray array];
    if ([PassValueClass sharedIntance].nearDriversArrsy.count > 0)
    {
        _dataArray = [PassValueClass sharedIntance].nearDriversArrsy;
    }

    _detaiView.backgroundColor = [[UIColor darkTextColor] colorWithAlphaComponent:0.65f];
    [_detaiView_Small.layer setCornerRadius:10];
    [_detaiView_Small.layer setBorderColor:[[UIColor whiteColor] CGColor]];//描边颜色
    [_detaiView_Small.layer setBorderWidth:2.0];//描边粗细
    [self addSubview:_detaiView];
    _detaiView.hidden = YES;
}

#pragma mark - TableViewDataSource Delegate - TableView Datasource代理

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [_dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NearDriverCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NearDriverCell"];
    if (!cell)
    {
        cell = [NearDriverCell getInstance];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setSelected:YES animated:YES];
    }
    if (_dataArray.count > 0)
    {
        NSDictionary *dict = [_dataArray objectAtIndex:indexPath.row];
        NSLog(@"dict = %@",dict);
        
        cell.showDetailsBtn.tag = indexPath.row;
        [cell.showDetailsBtn addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
        
        cell.driverPhoneBtn.tag = indexPath.row;
        [cell.driverPhoneBtn addTarget:self action:@selector(callTheDriver:) forControlEvents:UIControlEventTouchUpInside];
        
        NSString *nameStr      = [dict objectForKey:kNick];
        if (nameStr.length > 0)
        {
            cell.nameLbl.text = nameStr;//司机名称
        }
                
        NSString *carStatusStr = [dict objectForKey:kStatus];
        if (carStatusStr.length == 1)
        {
            if ([carStatusStr isEqualToString:@"0"])
            {
              cell.statusImgView.image = [UIImage imageNamed:@"emptydriverpic.png"];
            }
            else if ([carStatusStr isEqualToString:@"1"])
            {
              cell.statusImgView.image = [UIImage imageNamed:@"emptydriverpic.png"];

            }
            else if ([carStatusStr isEqualToString:@"2"])
            {
                cell.statusImgView.image = [UIImage imageNamed:@"fulldriverpic.png"];
            }
        }
        
        CLLocationCoordinate2D driverCo;
        NSString *lngStr   = [dict objectForKey:kLongitude];
        NSString *latStr   = [dict objectForKey:kLatitude];
        driverCo.latitude  = [latStr floatValue];
        driverCo.longitude = [lngStr floatValue];
        
        CLLocationCoordinate2D userCo;
        NSString *user_lat = [PassValueClass sharedIntance].tmpLatStr;
        NSString *user_lng = [PassValueClass sharedIntance].tmpLngStr;
        userCo.latitude  = [user_lat floatValue];
        userCo.longitude = [user_lng floatValue];
        
        NSString *newStr;
        
        double dis = [self getCLLocationDistance:userCo TheTowCoordinate:driverCo]/1000;
        NSString *disStr = [NSString stringWithFormat:@"%f",dis];
        
        if ([disStr length] > 6)
        {
            newStr = [disStr substringToIndex:5];
            
            cell.carDistanceLbl.text = newStr;
        }
        else
        {
            cell.carDistanceLbl.text = newStr;
        }
        
        NSString *carNoStr = [dict objectForKey:kHPHM];
        if (carNoStr.length > 0)
        {
            cell.carNoLbl.text = carNoStr;
        }
        
        NSString *driverCreditStr   = [dict objectForKey:kCredit];
        if (driverCreditStr.length > 0)
        {
            cell.driverCreditLbl.text = driverCreditStr;
        }
    }
    return cell;
}

- (void)callTheDriver:(UIButton *)sender
{
    if (isTiYanMoShiOk)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前处在体验模式" message:@"体验模式下仅能查看信息" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }

    int i = sender.tag;
    NSDictionary *dict = [_dataArray objectAtIndex:i];

    NSString *phoneStr     = [dict objectForKey:kTelephone];//司机电话
    
    if (phoneStr.length > 0 && [phoneStr length] == 11)
    {
        UIWebView*callWebview =[[UIWebView alloc] init];
        NSString *str = [NSString stringWithFormat:@"tel:%@",phoneStr];
        NSURL *telURL =[NSURL URLWithString:str];// 貌似tel:// 或者 tel: 都行
        [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
        //记得添加到view上
        [self addSubview:callWebview];
    }
}


- (void)showDetails:(UIButton *)sender
{
    int i = sender.tag;

    _detaiView.hidden = NO;
    
    //1.清空上一组数据
    [self cleanData];
    
    //2.加载页面和数据
    [self addData:i];
}

- (void)cleanData
{
    _nameLbl.text = nil;
    _statusLbl.text = nil;
    _carNoLbl.text = nil;
    _nameLbl.text = nil;
    _goodEvaluationLbl.text = nil;
    _badEvaluationLbl.text = nil;
}

- (void)addData:(NSInteger)i
{
    
    NSDictionary *dict = [_dataArray objectAtIndex:i];
    NSLog(@"dict = %@",dict);
    
    _callDriverBtn.tag = i;

    NSString *nameStr      = [dict objectForKey:kNick];
    if (nameStr.length > 0)
    {
        _nameLbl.text = nameStr;//司机名称
    }
    
    NSString *carStatusStr = [dict objectForKey:kStatus];
    if (carStatusStr.length == 1)
    {
        if ([carStatusStr isEqualToString:@"1"])
        {
            _statusLbl.text = @"空车";
            
        }
        else if ([carStatusStr isEqualToString:@"2"])
        {
            _statusLbl.text = @"载客";
        }
    }
    
/*    CLLocationCoordinate2D driverCo;
    NSString *lngStr   = [dict objectForKey:kLongitude];
    NSString *latStr   = [dict objectForKey:kLatitude];
    driverCo.latitude  = [latStr floatValue];
    driverCo.longitude = [lngStr floatValue];
    
    CLLocationCoordinate2D userCo;
    NSString *user_lat = [PassValueClass sharedIntance].tmpLatStr;
    NSString *user_lng = [PassValueClass sharedIntance].tmpLngStr;
    userCo.latitude  = [user_lat floatValue];
    userCo.longitude = [user_lng floatValue];
    
    NSString *newStr;
    
    double dis = [self getCLLocationDistance:userCo TheTowCoordinate:driverCo]/1000;
    NSString *disStr = [NSString stringWithFormat:@"%f",dis];
    
    if ([disStr length] > 6)
    {
        newStr = [disStr substringToIndex:5];
        
        cell.carDistanceLbl.text = newStr;
    }
    else
    {
        cell.carDistanceLbl.text = newStr;
    }
    */
    
    NSString *carNoStr = [dict objectForKey:kHPHM];
    if (carNoStr.length > 0)
    {
        _carNoLbl.text = carNoStr;
    }
    
    NSString *driverCreditStr   = [dict objectForKey:kCredit];
    if (driverCreditStr.length > 0)
    {
        _creditLbl.text = driverCreditStr;
    }

    NSString *goodEvaluationStr = [dict objectForKey:kGoodEvaluate];
    if (goodEvaluationStr.length > 0)
    {
        _goodEvaluationLbl.text = goodEvaluationStr;
    }
    
    NSString *badEvaluationStr  = [dict objectForKey:kBadEvaluate];
    if (badEvaluationStr.length > 0)
    {
        _badEvaluationLbl.text = badEvaluationStr;
    }
}

- (IBAction)removeThisView
{
    _detaiView.hidden = YES;
}

- (IBAction)callThisDriver:(UIButton *)sender
{
    if (isTiYanMoShiOk)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前处在体验模式" message:@"体验模式下仅能查看信息" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    
    int i = sender.tag;
    NSDictionary *dict = [_dataArray objectAtIndex:i];
    
    NSString *phoneStr     = [dict objectForKey:kTelephone];//司机电话
    
    if (phoneStr.length > 0 && [phoneStr length] == 11)
    {
        UIWebView*callWebview =[[UIWebView alloc] init];
        NSString *str = [NSString stringWithFormat:@"tel:%@",phoneStr];
        NSURL *telURL =[NSURL URLWithString:str];// 貌似tel:// 或者 tel: 都行
        [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
        //记得添加到view上
        [self addSubview:callWebview];
    }
}

#pragma mark - Baidu - 百度代理

- (CLLocationDistance)getCLLocationDistance:(CLLocationCoordinate2D)coordinateA TheTowCoordinate:(CLLocationCoordinate2D )coordinateB
{
    CLLocationDistance dis;
    dis = BMKMetersBetweenMapPoints(BMKMapPointForCoordinate(coordinateA), BMKMapPointForCoordinate(coordinateB)) ;
    return dis;
}

@end
